package servlet;

import anno.Autowired;
import anno.RequestBody;
import anno.ResponseBody;
import dao.AttendanceDAO;
import pojo.Attendance;
import utils.ApiResult;
import utils.JDBCUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.Map;

@WebServlet("/attendance")
public class AttendanceServlet extends BaseServlet {

    @Autowired
    private AttendanceDAO attendanceDAO;

    /**
     * 分页模糊查询考勤记录
     *
     * @param req       HttpServletRequest对象
     * @param resp      HttpServletResponse对象
     * @param teacherId 教师ID，如果为null，则查询所有教师的考勤记录
     * @param keyword   关键词，用于模糊查询
     * @param pageNum   页码
     * @param pageSize  每页记录数
     * @return ApiResult对象，包含查询结果
     * @throws SQLException 数据库异常
     */
    @ResponseBody
    public ApiResult findAttendancesByPageAndName(HttpServletRequest req, HttpServletResponse resp,
                                                  String teacherId, String keyword, String pageNum, String pageSize) throws SQLException {
        int page = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        Integer teacherIdInt = teacherId != null ? Integer.parseInt(teacherId) : null;
        JDBCUtils.Page<Attendance> result = attendanceDAO.findAttendancesByPageAndName(teacherIdInt, keyword, page, size);
        return new ApiResult(200, "分页模糊查询考勤记录成功", result);
    }

    /**
     * 插入新的考勤记录
     *
     * @param req        HttpServletRequest对象
     * @param resp       HttpServletResponse对象
     * @param attendance 考勤记录对象
     * @return ApiResult对象，包含插入结果
     * @throws SQLException 数据库异常
     */
    @ResponseBody
    public ApiResult insertAttendance(HttpServletRequest req, HttpServletResponse resp, @RequestBody Attendance attendance) throws SQLException {
        Integer id = attendanceDAO.insertAttendance(attendance);
        return new ApiResult(200, "插入考勤记录成功", id);
    }
    /**
     * 根据考勤ID查询该出勤记录表中，status比例，以及分别是谁
     *
     * @param req          HttpServletRequest对象
     * @param resp         HttpServletResponse对象
     * @param attendanceId 考勤记录ID
     * @return ApiResult对象，包含查询结果
     * @throws SQLException 数据库异常
     */
    @ResponseBody
    public ApiResult getAttendanceStatusRatio(HttpServletRequest req, HttpServletResponse resp, String attendanceId) throws SQLException {
        int attendanceIdInt = Integer.parseInt(attendanceId);
        Map<String, Object> result = attendanceDAO.getAttendanceStatusRatio(attendanceIdInt);
        return new ApiResult(200, "查询考勤记录状态比例成功", result);
    }

    /**
     * 更新考勤记录
     *
     * @param req        HttpServletRequest对象
     * @param resp       HttpServletResponse对象
     * @param attendance 考勤记录对象
     * @return ApiResult对象，包含更新结果
     * @throws SQLException 数据库异常
     */
    @ResponseBody
    public ApiResult updateAttendance(HttpServletRequest req, HttpServletResponse resp, @RequestBody Attendance attendance) throws SQLException {
        boolean success = attendanceDAO.update(attendance);
        return new ApiResult(200, "更新考勤记录成功", success);
    }

    /**
     * 删除考勤记录
     *
     * @param req  HttpServletRequest对象
     * @param resp HttpServletResponse对象
     * @param id   考勤记录ID
     * @return ApiResult对象，包含删除结果
     * @throws SQLException 数据库异常
     */
    @ResponseBody
    public ApiResult deleteAttendance(HttpServletRequest req, HttpServletResponse resp, String id) throws SQLException {
        boolean success = attendanceDAO.deleteById(Integer.parseInt(id));
        return new ApiResult(200, "删除考勤记录成功", success);
    }

}